package com.elite.groovy.callback;

import com.elite.groovy.util.EliteBean;
import com.elite.groovy.kh.KHDBConfig;
import com.elite.groovy.kh.KHUtil;
import com.elite.groovy.util.Util;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @Title: 项目Token相关统一管理接口* @author: LJT*
 *
 */

public class apiGetToken implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiGetToken.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");

        // 调用IP过滤
        if (KHDBConfig.getParam(eliteBean, "#APIIP") == "1" && !KHUtil.getRemoteHost(request)) {
            log.warn("API被非法IP调用-Request Not Allow!");
            return "Request Not Allow!";
        }

        // 获取数据源List<Map<String, Object>>
        JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());

        String getMethod = request.getMethod();
        log.debug("getMethod:" + getMethod);

        JSONObject result = new JSONObject();
        JSONObject datatoken = new JSONObject();

        String APP_ID = "";

        if (getMethod.equals("GET")) {
            APP_ID = request.getParameter("appid");
            log.debug("原始Get请求参数:" + APP_ID);
        } else {
            InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
            BufferedReader iny = new BufferedReader(reader);
            String line;
            while ((line = iny.readLine()) != null) {
                APP_ID += line;
            }
            log.debug("原始Post请求参数:" + APP_ID);
        }

        //  入参为空
        if (KHUtil.isNull(APP_ID)) {
            log.debug("APP_ID入参为空-无法完成操作!");
            result.put("code", -2);
            result.put("message", "APP_ID入参为空-无法完成操作!");

            return result;
        }

        try {
            // 检查当前Token状态
            String selectSQL = "select token, expired from kh_accesstoken where app_id = ? and del_flag = 0 and date_add(createdtime,interval expired second) > now() limit 1";
            List<Map<String, Object>> selectExits = jt.queryForList(selectSQL, APP_ID);
            if (selectExits.size() > 0) {
                Map selectExitsm = selectExits.get(0);
                String TOKEN = selectExitsm.get("token");
                Long EXPIRED = Long.parseLong(selectExitsm.get("expired").toString());

                result.put("code", 1);
                result.put("message", "查询完成");
                datatoken.put("access_token", TOKEN);
                datatoken.put("expires_in", EXPIRED);
                result.put("value", datatoken);

            } else {
                Map<String, String> tokenMap = new HashMap<>();
                /*
                WXM开头 微信小程序
                WXA开头 微信公众号
                WXG开头 企业微信
                */

                if (APP_ID.indexOf("WXM") == 0) {
                    tokenMap = getWXMToken(eliteBean, APP_ID);
                } else if (APP_ID.indexOf("WXA") == 0) {
                    tokenMap = getWXToken(eliteBean, APP_ID);
                } else if (APP_ID.indexOf("WXG") == 0) {
                    tokenMap = getWXToken(eliteBean, APP_ID);
                } else {
                    log.warn("获取Token类型不支持");
                    result.put("code", -2);
                    result.put("message", "获取Token类型不支持");
                    result.put("value", datatoken);
                    return result.toString();
                }

                String TOKEN = tokenMap.get("access_token");
                Long EXPIRED = Long.parseLong(tokenMap.get("expires_in").toString());

                dealToDB(jt, APP_ID, TOKEN, EXPIRED);

                result.put("code", 1);
                result.put("message", "更新完成");
                datatoken.put("access_token", TOKEN);
                datatoken.put("expires_in", EXPIRED);
                result.put("value", datatoken);

            }

        } catch (Exception e) {
            log.error("接口处理异常", e);
            // 失败返回

            result.put("code", -1);
            result.put("message", e.getMessage());
            result.put("value", datatoken);

        }

        log.debug("Token处理完成返回结果:" + result.toString());

        return result.toString();
    }

    // 将Token更新到DB
    public static void dealToDB(JdbcTemplate jt, String APP_ID, String access_token, Long expires_in) {

        // 清理超长时间的Token
        String dealSQL = "Delete From KH_Accesstoken Where Createdtime < Date_Add(Now(), Interval - 1 Day) And APP_ID = ?";
        jt.update(dealSQL, APP_ID);

        // 标识近期内Token无效
        dealSQL = "Update KH_Accesstoken Set Del_Flag = 1 Where Del_Flag = 0 And APP_ID = ?";
        jt.update(dealSQL, APP_ID);

        // 将新获取的Token入库存储
        dealSQL = "Insert Into Kh_Accesstoken (Rec_Id, Token, Createdtime, Expired, Del_Flag, APP_ID) Values (UUID(), ?, Now(), ?, 0, ?)";
        jt.update(dealSQL, access_token, expires_in, APP_ID);
    }

    // 获取企微&微信公众号Token
    public static Map<String, String> getWXToken(EliteBean eliteBean, String APP_ID) {

        Map<String, String> tokenMap = new HashMap<String, String>();

        // 调用微信获取最新Token
        String wxTokenURL = KHDBConfig.getAPIURL(eliteBean, "APIToken-" + APP_ID).replace("#{corpid}", KHDBConfig.getParamConfig(eliteBean, "wxq-appID")).replace("#{corpsecret}", KHDBConfig.getParamConfig(eliteBean, "wxq-appSecret"));

        log.debug("调用API获取最新TokenURL:" + wxTokenURL);

        String wxTokenResult = Util.doHttpGet(wxTokenURL, "UTF-8", 5000, 0);
        log.debug("调用API获取最新Token返回：" + wxTokenResult);
        JSONObject wxTokenJson = JSONObject.fromObject(wxTokenResult);

        if (wxTokenJson.has("access_token")) {
            try {

                String access_token = wxTokenJson.getString("access_token");
                String expires_in = wxTokenJson.getString("expires_in");
                tokenMap.put("access_token", access_token);
                tokenMap.put("expires_in", expires_in);

            } catch (Exception e) {
                log.error(e);
            }
        }

        return tokenMap;
    }

    // 获取微信小程序Token
    public static Map<String, String> getWXMToken(EliteBean eliteBean, String APP_ID) {

        Map<String, String> tokenMap = new HashMap<String, String>();

        // 调用微信获取最新Token
        String wxTokenURL = KHDBConfig.getAPIURL(eliteBean, "APIToken-" + APP_ID);

        log.debug("调用API获取最新TokenURL:" + wxTokenURL);

        String wxTokenResult = Util.doHttpGet(wxTokenURL, "UTF-8", 5000, 0);
        log.debug("调用API获取最新Token返回：" + wxTokenResult);
        JSONObject wxTokenJson = JSONObject.fromObject(wxTokenResult);

        try {

            String access_token = wxTokenJson.getString("access_token");
            String expires_in = wxTokenJson.getString("expires_in");
            tokenMap.put("access_token", access_token);
            tokenMap.put("expires_in", expires_in);

        } catch (Exception e) {
            log.error(e);
        }

        return tokenMap;
    }

}